<template>
  <el-dialog
    title="新增员工"
    :visible.sync="dialogShow"
    width="width"
    @close="colseDialog"
  >
    <!-- //!绘制表单 -->
    <el-form
      v-if="dialogShow"
      ref="form"
      :model="formData"
      :rules="formRules"
      label-width="140px"
    >
      <el-form-item label="姓名" prop="username" size="normal">
        <el-input
          v-model="formData.username"
          style="width: 320px"
          placeholder="请输入姓名"
        />
      </el-form-item>
      <el-form-item label="手机" prop="mobile" size="normal">
        <el-input
          v-model="formData.mobile"
          style="width: 320px"
          placeholder="请输入手机"
        />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry" size="normal">
        <el-date-picker
          v-model="formData.timeOfEntry"
          style="width: 320px"
          placeholder="请输入入职时间"
        />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment" size="normal">
        <el-select
          v-model="formData.formOfEmployment"
          style="width: 320px"
          placeholder="请输入聘用形式"
        >
          <el-option
            v-for="item in hireType"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber" size="normal">
        <el-input
          v-model="formData.workNumber"
          style="width: 320px"
          placeholder="请输入工号"
        />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName" size="normal">
        <el-input
          v-model="formData.departmentName"
          style="width: 320px"
          placeholder="请输入部门"
          @focus="getDeptList"
        />
        <!-- //!部门树 -->
        <el-tree
          v-if="treeShow"
          node-key="id"
          :data="deptList"
          style="width: 320px"
          :props="{ label: 'name' }"
          @node-click="nodeClick"
        />
      </el-form-item>
      <el-form-item label="转正时间" prop="username" size="normal">
        <el-date-picker
          v-model="formData.correctionTime"
          style="width: 320px"
          placeholder="请输入转正时间"
        />
      </el-form-item>
      <el-row :gutter="10">
        <el-button type="primary" @click="dialogShow = false">取消</el-button>
        <el-button type="primary" @click="btnOk">确认</el-button>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script>
import employeesEnum from '@/api/constant/employees'
import { getDepartmentListApi } from '@/api/department'
import { tranListToTreeData } from '@/utils'
import { addEmployeesApi } from '@/api/employees'
export default {
  name: 'AddEmployees',
  data() {
    return {
      // 三个变量
      // 弹框控制变量
      dialogShow: false,
      // 表单数据变量
      formData: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      // 表单规则变量
      formRules: {
        username: [{ required: true, message: '请输入', trigger: 'blur' }],
        mobile: [{ required: true, message: '请输入', trigger: 'blur' }],
        formOfEmployment: [
          { required: true, message: '请选择', trigger: 'change' }
        ],
        workNumber: [{ required: true, message: '请输入', trigger: 'blur' }],
        departmentName: [
          { required: true, message: '请输入', trigger: 'change' }
        ],
        timeOfEntry: [{ required: true, message: '请选择', trigger: 'blur' }],
        correctionTime: [{ required: true, message: '请选择', trigger: 'blur' }]
      },
      // 聘用形式
      hireType: employeesEnum.hireType,
      // 部门数据
      deptList: [],
      // 布尔值控制树显示
      treeShow: false
    }
  },
  methods: {
    async getDeptList() {
      const { depts } = await getDepartmentListApi()
      this.deptList = tranListToTreeData(depts, '')
      this.treeShow = true
    },
    nodeClick(val) {
      this.formData.departmentName = val.name
      this.treeShow = false
    },
    async btnOk() {
      try {
        await this.$refs.form.validate()
      } catch (e) {
        return console.log(e)
      }
      await addEmployeesApi(this.formData)
      this.$message.success('新增成功')
      this.$emit('updataList')
      this.dialogShow = false
    },
    colseDialog() {
      this.formData = {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      }
      this.$refs.form.clearValidate()
    }
  }
}
</script>

<style></style>
